# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
@Time : 2022/10/3 0003 11:53
@Author : 测试开发工程师
@File : 27. 使用 curl 发送请求.py
@Software: PyCharm
============================
"""

"""
F12中：
    Network -> All ：抓取接口信息
    Network -> XHR : 查看异构请求信息
    
HTTP 协议组成
    target: url protocol(http/https) host port
    request:
        request: get post put delete  head
        header: host cookie user-agent
        get query
        post body: json xml form
        
    response:
        status line
        header
        body

通过 TCP/IP 数据包发送
浏览器选择接口 - copy as curl
    把浏览器发送的请求真实的还原出来
    附带了认证信息，所以可以脱离浏览器执行
    可以方便开发者重放请求，修改参数调式，编写脚本

客户端模拟请求工具：
    nc tcp/udp协议发送
    curl 最常用的 http 请求工具（开发常用）
    postman 综合性的 http 协议测试工具
    代理工具，IDE 工具，浏览器插件工具
     
# 加上 |jq 命令，以 json 格式输出
curl 'https://xueqiu.com/query/v1/search/compliance.json?q=sogo' \
>   -H 'Accept: application/json, text/plain, */*' \
>   -H 'Accept-Language: zh-CN,zh;q=0.9' \
>   -H 'Connection: keep-alive' \
>   -H 'Cookie: acw_tc=2760827316647692976963876ef76604690146a66d5fa585441bfacd86af7e; xq_a_token=25916c3bfec27272745f6070d664a48d4b10d322; xqat=25916c3bfec27272745f6070d664a48d4b10d322; xq_r_token=2242d232b1aa6ffb6d9569d53e067311db16c12c; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY2NzAwMjE2NSwiY3RtIjoxNjY0NzY5MjYxNTU1LCJjaWQiOiJkOWQwbjRBWnVwIn0.Oa5x9ynRZqNy3QXSnedHdYjDVNpFumKBBzgr9Wn4Nm-Xb22oMagZuNmfnov5StUzSYRsIQ_S1-eQeECna8ZhZg5sH1XnGS1vIQ_MdUAOnIA_MayIsdoYaz4U86OYbGaCr_WnDg8VXW5VMABLPdpWoVo5ApEA027oMe3kHJ-9E_It2g6x1GXbcJTsTxhXsED2YpD5JeSUifYKYthdJ-43ymj18G-BVZF-hn6hyDXo8gkBV_bO8JaAxGScR-5dyFY3Lr-4mrbJzEQPFtIjxnr9ymjmz6qA6JG0Xh-iIpgeSYlgFXaYKcI3234QoQOrTMMDO_cZwnSVAhgqtgmZ3-sWUA; u=661664769297703; Hm_lvt_1db88642e346389874251b5a1eded6e3=1664769301; device_id=9ea434a20be2182bbfdd2d71bbd35c32; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1664769497' \
>   -H 'Referer: https://xueqiu.com/k?q=sogo' \
>   -H 'Sec-Fetch-Dest: empty' \
>   -H 'Sec-Fetch-Mode: cors' \
>   -H 'Sec-Fetch-Site: same-origin' \
>   -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
>   -H 'elastic-apm-traceparent: 00-ce5d4f9c1e634cc4bdf0e586f4784aa3-df8bcf024c8a2be8-00' \
>   -H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
>   -H 'sec-ch-ua-mobile: ?0' \
>   -H 'sec-ch-ua-platform: "Windows"' \
>   --compressed

结果返回：
    {"code":200,"data":[],"message":"success","meta":{"count":0,"feedback":-1,"has_next_page":true,"maxPage":1,"page":1,"query_id":1576786141003350016,"size":0},"success":true}[root@VM-4-15-centos ~]#


curl 常见用法：
    url = http://www,baidu.com
    get 请求 curl $url
    post 请求 curl -d 'xxx' $url
    proxy 使用 ： curl -x 'http://127.0.0.1:8080' $url
    
curl 重要参数：
    示例介绍： https://home.testing-studio.com/t/topic/1065
    -H  'Content-Type: application/json'  消息头设置
    -u username:password  用户认证
    -d 要发送的 post 数据@file 表示来自于文件
    --data-urlencode 'page_size=50' 对内容进行 url 编码
    -G 把 data 数据当成 get 请求的参数发送，长与 --data-urlencode 结合使用
    -o 写文件
    -x 代理 http 代理，socks5 代理
    -v verbose 打印更详细日志 
    -s 关闭一些提示输出
    

例子：
url=http://www.baidu.com

## get请求加json解析 
curl -s 'https://xueqiu.com/stock/search.json?code=sogo&size=3&page=1' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'Accept: application/json, text/plain, */*' -H 'Sec-Fetch-Dest: empty' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' -H 'elastic-apm-traceparent: 00-760301b0a132e9a4c0f5ac7448a3419e-8823be75504fc61f-00' -H 'Sec-Fetch-Site: same-origin' -H 'Sec-Fetch-Mode: cors' -H 'Referer: https://xueqiu.com/k?q=sogo' -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'Cookie: device_id=24700f9f1986800ab4fcc880530dd0ed; cookiesu=841584103115161; aliyungf_tc=AQAAAIPytE8aVQoAXhjf3cw3R+j5DD/s; acw_tc=2760824b15851452106833674e25941ad47588d5d7ded79b38a04dad8f9444; xq_a_token=2ee68b782d6ac072e2a24d81406dd950aacaebe3; xqat=2ee68b782d6ac072e2a24d81406dd950aacaebe3; xq_r_token=f9a2c4e43ce1340d624c8b28e3634941c48f1052; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTU4NzUyMjY2MSwiY3RtIjoxNTg1MTQ1MTYxMDIwLCJjaWQiOiJkOWQwbjRBWnVwIn0.TPrw6_M2Th9QTVz5spwUybqN1790nJANu9kxXl4GfNb1eQ2p2zD43CStgogOGQ8yRXYmSCfURp0343wgjnnCdnQX5698Jl-brdP94wiYKwv11q8QjBYMXFWJGRj0g69C2nxVrRF8K-ETGEked3KjYfk8Xy2wPuZtyGUhORWeCvMhmBdcRKIlWj4d7wp-w_LjMbSLigJAT29F03wBZIxR0r3eMNUhUsXh8dCsWNb6wzhtg8dT4gcd91mQmR5ToR_SFrzQfOopY4vQGcaOHWaAwUMPLUopZwD4ajWzm1kpoBZnf_n_9uBfT4j0nGk95E8J8EmTfBlq-1p019xkhgp87w; u=431585145210698; Hm_lvt_1db88642e346389874251b5a1eded6e3=1583285031,1584102200,1585145180; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1585145192' --compressed | jq
{
  "q": "sogo",
  "page": 1,
  "size": 3,
  "stocks": [
    {
      "code": "SOGO",
      "name": "搜狗",
      "enName": "",
      "hasexist": "false",
      "flag": null,
      "type": 0,
      "stock_id": 1029472,
      "ind_id": 0,
      "ind_name": "通讯业务",
      "ind_color": null,
      "_source": "sc_1:1:sogo"
    }
  ]
}


#post请求
curl 'http://sonarqube.testing-studio.com:9000/api/authentication/login' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'Accept: application/json' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Origin: http://sonarqube.testing-studio.com:9000' -H 'Referer: http://sonarqube.testing-studio.com:9000/sessions/new' -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'Cookie: _ga=GA1.2.232181868.1566982077; experimentation_subject_id=IjNlYzgxODQ1LTU2MDAtNGIyNy1iNTgzLTE1MzRkY2IwMDI0ZSI%3D--b1f29d33f6a2c85a81be66e4774d437f710c102f; _gid=GA1.2.482544306.1585051015' --data 'login=admin&password=1234' --compressed --insecure

#百度的一个url提交脚本
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls"

#对参数编码并发送get请求
    curl -G $url  \
        --data-urlencode "current=$current" \
        --data-urlencode "pageSize=$pageSize" 


#认证与put上传都ElasticSearch里
    curl -X PUT "$ES_HOST/$index/_doc/$id?pretty" \
        --user username:password \
        -H 'Content-Type: application/json' \
        -d "$content"


#查看邮箱
curl -s --user $mail_username:$mail_password "imaps://imap.exmail.qq.com/inbox?all"



"""
